package org.jeecg.modules.friends.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.friends.base.HttpReturn;
import org.jeecg.modules.friends.base.PageRsp;
import org.jeecg.modules.friends.base.S;
import org.jeecg.modules.friends.service.NewMomentsService;
import org.jeecg.modules.friends.service.req.BasePageReq;
import org.jeecg.modules.friends.service.req.MomentsReq;
import org.jeecg.modules.friends.service.req.PublishMomentsReviewReq;
import org.jeecg.modules.friends.service.req.SelectedPageListReq;
import org.jeecg.modules.friends.service.req.SetHiddenReq;
import org.jeecg.modules.friends.service.req.SetSelectedReq;
import org.jeecg.modules.friends.service.rsp.MomentReviewRsp;
import org.jeecg.modules.friends.service.rsp.MomentsInfoRsp;
import org.jeecg.modules.friends.service.rsp.MomentsRsp;
import org.jeecg.modules.friends.service.rsp.UnReadCountRsp;
import org.jeecg.modules.friends.user.entity.User;
import org.jeecg.modules.friends.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * Created on 2025-01-21
 */
@Slf4j
@RestController
@RequestMapping("/new/moment/")
public class NewMomentsController {
    @Autowired
    private IUserService userService;
    @Autowired
    private NewMomentsService newMomentsService;

    // 获取精选动态
    @RequestMapping(value = "selected/page/list", method = RequestMethod.GET)
    public HttpReturn<PageRsp<MomentsInfoRsp>> selectedPageList(SelectedPageListReq req,
            @RequestHeader(name = "token", required = false) String token) {
        User user = null;
        if (StrUtil.isNotBlank(token)) {
            user = userService.getUserByPhone(JwtUtil.getMobile(token));
            S.assertIsNotNull(user, "登录过期，请重新登录");
        }
        return newMomentsService.selectedPageList(req, user);
    }

    // 动态详情
    @RequestMapping(value = "get/moments/info", method = RequestMethod.GET)
    public HttpReturn<MomentsInfoRsp> getMomentsInfo(SelectedPageListReq req,
            @RequestHeader(name = "token", required = false) String token) {
        User user = null;
        if (StrUtil.isNotBlank(token)) {
            user = userService.getUserByPhone(JwtUtil.getMobile(token));
            S.assertIsNotNull(user, "登录过期，请重新登录");
        }
        HttpReturn<PageRsp<MomentsInfoRsp>> pageRspHttpReturn = newMomentsService.selectedPageList(req, user);
        List<MomentsInfoRsp> data = pageRspHttpReturn.getResult().getData();
        if (CollectionUtil.isNotEmpty(data)) {
            return HttpReturn.successResult(data.get(0));
        }
        return HttpReturn.failResult("动态不存在");
    }

    // 发布评论
    @RequestMapping(value = "publish/momentsReview", method = RequestMethod.POST)
    public HttpReturn<MomentReviewRsp> publishMomentsReview(@RequestBody PublishMomentsReviewReq req,
            @RequestHeader(name = "token") String token) {
        User user = userService.getUserByPhone(JwtUtil.getMobile(token));
        S.assertIsNotNull(user, "登录过期，请重新登录");
        req.validate();
        newMomentsService.publishMomentsReview(req, user);
        MomentReviewRsp rsp = new MomentReviewRsp();
        rsp.setNickName(user.getNickname());
        rsp.setReviewContent(req.getReviewContent());
        rsp.setUserId(user.getId());
        rsp.setHeadPicUrl(user.getHeadPicUrl());
        rsp.setGender(user.getGender());
        rsp.setAge(user.getBirthday());
        rsp.setCurrentResidence(user.getCurrentResidence());
        rsp.setAtUserId(req.getAtUserId());
        rsp.setAtUserNickName(req.getAtUserName());
        return HttpReturn.successResult(rsp);
    }

    // 获取未读消息
    @RequestMapping(value = "/get/un/read/Moments", method = RequestMethod.GET)
    public HttpReturn<UnReadCountRsp> getUnReadMoments(BasePageReq req,
            @RequestHeader(name = "token", required = false) String token) {
        if (StringUtils.isEmpty(token)) {
            return HttpReturn.successResult();
        }
        User user = userService.getUserByPhone(JwtUtil.getMobile(token));
        S.assertIsNotNull(user, "登录过期，请重新登录");
        return HttpReturn.successResult(newMomentsService.getUnReadMoments(user));
    }

    // 获取未读消息数量
    @RequestMapping(value = "/get/un/read/counts", method = RequestMethod.GET)
    public HttpReturn<Long> getUnReadMomentCounts(BasePageReq req,
            @RequestHeader(name = "token", required = false) String token) {
        if (StringUtils.isEmpty(token)) {
            return HttpReturn.successResult();
        }
        User user = userService.getUserByPhone(JwtUtil.getMobile(token));
        S.assertIsNotNull(user, "登录过期，请重新登录");
        return HttpReturn.successResult(newMomentsService.getUnReadMomentsCount(user));
    }

    // 后台列表查询
    @RequestMapping(value = "moments/page/list", method = RequestMethod.GET)
    public HttpReturn<PageRsp<MomentsRsp>> momentsPageList(MomentsReq req) {
        return newMomentsService.momentsPageList(req);
    }


    // 设置/取消 精选
    @RequestMapping(value = "set/selected", method = RequestMethod.POST)
    public HttpReturn<?> setSelected(@RequestBody SetSelectedReq req) {
        newMomentsService.setSelected(req);
        return HttpReturn.successResult();
    }

    // 设置/取消 隐藏
    @RequestMapping(value = "set/hidden", method = RequestMethod.POST)
    public HttpReturn<?> setHidden(@RequestBody SetHiddenReq req) {
        newMomentsService.setHidden(req);
        return HttpReturn.successResult();
    }
}

